﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Physis.Client.Models.Configuration {

    public class EnvironmentModel  : ConfigurationItemModel {
                
        #region Private Properties

        private Enterprise.Environment environment = null;

        #endregion


        #region Public Properties

        public override Int64 ConfigurationItemId { set { EnvironmentId = value; } }

        public override Core.CoreConfigurationObject ConfigurationItem { get { return environment; } }


        public Int64 EnvironmentId {

            set {

                environment = Application.Enterprise.Environment (value);

                if (environment == null) {

                    environment = new Enterprise.Environment (Application);

                }

            }

        }

        public Enterprise.Environment Environment { get { return environment; } set { environment = value; } }

        #endregion 


        #region Constructors

        public EnvironmentModel (Int64 forEnvironmentId) {

            EnvironmentId = forEnvironmentId;

            return;

        }

        public EnvironmentModel (System.Collections.Specialized.NameValueCollection form) {

            UpdateValues (form);

            return;

        }

        #endregion 


        #region Public Methods

        public override void UpdateValues (System.Collections.Specialized.NameValueCollection form) {

            base.UpdateValues (form);


            Environment.DatabaseConfiguration.DatabaseType = (Data.Enumerations.DatabaseType)Data.Readers.FormReader.Int32FromForm (form, "Environment.DatabaseConfiguration.DatabaseType");

            Environment.DatabaseConfiguration.ServerName = Data.Readers.FormReader.StringFromForm (form, "Environment.DatabaseConfiguration.ServerName");

            Environment.DatabaseConfiguration.DatabaseName = Data.Readers.FormReader.StringFromForm (form, "Environment.DatabaseConfiguration.DatabaseName");

            Environment.DatabaseConfiguration.PoolingEnabled = Data.Readers.FormReader.BooleanFromForm (form, "Environment.DatabaseConfiguration.PoolingEnabled");

            Environment.DatabaseConfiguration.PoolSizeMinimum = Data.Readers.FormReader.Int32FromForm (form, "Environment.DatabaseConfiguration.PoolSizeMinimum");

            Environment.DatabaseConfiguration.PoolSizeMaximum = Data.Readers.FormReader.Int32FromForm (form, "Environment.DatabaseConfiguration.PoolSizeMaximum");

            Environment.DatabaseConfiguration.TrustedConnection = Data.Readers.FormReader.BooleanFromForm (form, "Environment.DatabaseConfiguration.TrustedConnection");

            Environment.DatabaseConfiguration.UserName = Data.Readers.FormReader.StringFromForm (form, "Environment.DatabaseConfiguration.UserName");

            Environment.DatabaseConfiguration.Password = Data.Readers.FormReader.StringFromForm (form, "Environment.DatabaseConfiguration.Password");

            Environment.DatabaseConfiguration.CustomAttributes = Data.Readers.FormReader.StringFromForm (form, "Environment.DatabaseConfiguration.CustomAttributes");

            return;

        }

        #endregion 

    }

}